<?php

/**
 * project actions.
 *
 * @package    vmdai
 * @subpackage project
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class projectActions extends sfActions
{
    
    public function executeIndex(sfWebRequest $request){
 
        $loan_id = $request->getParameter('id',0);
        $this->forward404Unless($this->one_result = Doctrine::getTable('Loan')->findOneById($loan_id));//loan信息
        $type= $this->one_result->getListType();
       
        $this->forward404Unless($type==6?false:true);//测试数据，不显示
        if($this->getUser()->isAuthenticated()){
            $user_id = $this->getUser()->getAttribute("user_id");
            $this->user = Doctrine::getTable('UserAccount')->findOneByUserId($user_id);//我的余额
        }
        
        $this->loan_user = Doctrine::getTable('User')->findOneById($this->one_result->getUserId());
        $this->loan_user_attr = Doctrine::getTable('UserAttr')->getUserAttrById($this->one_result->getUserId());
        $this->loan_description = Doctrine::getTable('LoanDescription')->getDescript($loan_id);
        $this->loan_record_list = Doctrine::getTable('LoanRecord')->getLoanList($loan_id);//投资记录
        $this->getResponse()->setTitle($this->one_result->getTitle()."_[投资项目] 投资理财,我要理财--微美贷VMdai官网_中国领先的互联网金融P2P网贷平台_网络贷款_投资理财");
        $user_attr = Doctrine::getTable('UserAttr')->findOneByUserId($this->one_result->getUserId());
        $userattrs=$user_attr->getUserAttrs();
        $this->edu_status=$user_attr::$edu_status[$userattrs['edu_status']];
        $this->marray_status=$user_attr::$marray_status[$userattrs['marray_status']];
        $this->house_infos=Doctrine::getTable('HouseInfos')->findOneByLoanId($loan_id);
        if( $request->isMethod("POST") ) {
            if(empty($user_id)){
                $this->getUser()->setFlash('error', '请先登录');
                $this->redirect('user/login');
            }
            if($user_id == $this->one_result->getUserId()){
                $this->getUser()->setFlash('error', '不能给自己投标');
                $this->redirect('project/index?id='.$loan_id);
            }
            $money = $request->getParameter("money",0);//投资金额
            
            if($this->one_result->getStatus() != 1){
                $this->getUser()->setFlash('error', '招标已经结束');
                $this->redirect('project/index?id='.$loan_id);
            }
           
            $day=$this->one_result->getEndTime();
            $create=$this->one_result->getDateTimeObject('created_at')->format('Y-m-d H:i:s');
            
            if($this->one_result->getStatus() != 1){
                $this->getUser()->setFlash('error', '招标已经结束');
                $this->redirect('project/index?id='.$loan_id);
            }else{
             
              if(time()>strtotime("$create +$day day")){
                $this->getUser()->setFlash('error', '招标已经结束');
                $this->redirect('project/index?id='.$loan_id);
              }
            }
            
            if($money < 100){
                $this->getUser()->setFlash('error', '投标金额必须>=100');
                $this->redirect('project/index?id='.$loan_id);
            }
            if($this->user->getRemainingMoney() <= 0){
                $this->getUser()->setFlash('error', '账户余额不足,请先充值');
                $this->redirect('project/index?id='.$loan_id);
            }
            
            
	    
            $surplus_monry = bcsub($this->one_result->getMoney(),$this->one_result->getRemainder(),  sfConfig::get('app_value_bit'));//剩余融资金额
            
            //总额的60%
            $money_div=bcmul($this->one_result->getMoney(),0.6, sfConfig::get('app_value_bit'));
            if( $money>$money_div){
                $this->getUser()->setFlash('error', '最大投标金额不能超过标的金额的60%');
                $this->redirect('project/index?id='.$loan_id);
            }
            $enter_money=$money;
            //由原来投标金额不能大于标剩余金额更改为投标金额大于剩余金额时，投标金额自动等于剩余金额
            if($money > $surplus_monry){
                $money = $surplus_monry;
            }

            $load_record_model = new LoanRecord();
            $type_id = $this->one_result->getListType();
            
            $result = $load_record_model->setLoanId($loan_id)->setTypeId($type_id)->setIsNew(true)->setUserId($user_id)->setLoanUserId($this->one_result->getUserId())->setLoanMoney($money)->setEnterMoney($enter_money)->save();
	    $this->getUser()->setFlash('success', '投标成功');
            $this->redirect('project/index?id='.$loan_id,302);
        }
    }
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
    public function executeList(sfWebRequest $request){
        $this->getResponse()->setTitle("[投资项目] 投资理财,我要理财--微美贷VMdai官网_中国领先的互联网金融P2P网贷平台_网络贷款_投资理财");
        $this->type_list = Doctrine::getTable('LoanListType')->findAll()->toKeyValueArray('id', 'name');//列表类型
        $this->date_line = array("全部","3个月以下","3-6个月","6-9个月","9-12个月");
        $this->status = loan::$status_list;
        if($this->getUser()->isAuthenticated()){
            $user_id = (int)$this->getUser()->getAttribute("user_id");
            $this->remaining_money = doctrine::getTable("UserAccount")->findOneByUserId($user_id);
        }
        $this->get_type = $request->getParameter("type",0);
        $this->get_dete_line = $request->getParameter('dete_line',0);
        $this->get_status = $request->getParameter('status',0);
         //获取列表
        $query = Doctrine::getTable('Loan')->getLoanList($this->get_type,$this->get_dete_line,$this->get_status);     
        $this->pager = new sfDoctrinePager('Loan',10);
        $page = $request->getParameter('page',1);
        $this->pager->setQuery($query);
        $this->pager->setPage($page);
        $this->pager->init();
    }
}
